from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
#By类，用于定位元素
from selenium.webdriver.common.by import By
#显示等待相关的类
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

service = Service("d:/chromedriver/chromedriver-win64/chromedriver.exe")
driver = webdriver.Chrome(service=service)
base_url = "https://www.baidu.com"
#隐式等待
driver.implicitly_wait(5)
#隐式等待和显示等待都存在，超时时间取二者中的最大值
driver.get(base_url)
#判断标题，如果符合，返回True
flag = WebDriverWait(driver, 10).until(EC.title_is("百度一下，你就知道"))
print(flag)
#判断标题，是否包含
flag = WebDriverWait(driver, 10).until(EC.title_contains("百度一下"))
#print(flag)
#查找百度的输入框
#res = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"kw")))
#res.send_keys("三支一扶")
#element = driver.find_element(By.ID, "su")

#保存到baidu.txt


#element.click() #点击元素
res = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".mnav")))
res = driver.find_element(By.CLASS_NAME, "mnav")
print(type(res))
print(res)
time.sleep(5)